package com.fyy.quartz.service.impl;

import com.fyy.quartz.service.QuartzJobService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author fyy
 * @Description
 * @Date 2020/12/23 10:57
 */
@Service
public class QuartzJobServiceImpl implements QuartzJobService {

    private static final Logger log = LoggerFactory.getLogger(QuartzJobServiceImpl.class);
    @Autowired
    private Scheduler scheduler;

    @Override
    public void addJob(String clazzName, String jobName, String groupName, String cronExp, Map<String, Object> param) {
        try {
            //构建job信息
            Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(clazzName);
            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName,groupName).build();
            //表达式调度构建器(即任务执行的时间)
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExp);
            //按新的cronExpression表达式构建一个新的trigger
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(jobName,groupName)
                    .withSchedule(scheduleBuilder)
                    .build();
            //获得JobDataMap，写入数据
            if(param != null){
                trigger.getJobDataMap().putAll(param);
            }
            scheduler.scheduleJob(jobDetail,trigger);
        } catch (Exception e) {
            log.error("创建任务失败",e);
        }
    }

    @Override
    public void pauseJob(String jobName, String groupName) {
        try {
            scheduler.pauseJob(JobKey.jobKey(jobName,groupName));
        } catch (SchedulerException e) {
            log.error("暂停任务失败",e);
        }
    }

    @Override
    public void resumeJob(String jobName, String groupName) {
        try {
            scheduler.resumeJob(JobKey.jobKey(jobName,groupName));
        } catch (SchedulerException e) {
            log.error("恢复任务失败",e);
        }
    }

    @Override
    public void runOnce(String jobName, String groupName) {
        try {
            scheduler.triggerJob(JobKey.jobKey(jobName,groupName));
        } catch (SchedulerException e) {
            log.error("立即运行一次定时任务失败",e);
        }
    }

    @Override
    public void updateJob(String jobName, String groupName, String cronExp, Map<String, Object> param) {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobName,groupName);
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if(cronExp != null){
                // 表达式调度构建器
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExp);
                // 按新的cronExpression表达式重新构建trigger
                trigger = trigger.getTriggerBuilder()
                        .withIdentity(jobName,groupName)
                        .withSchedule(scheduleBuilder)
                        .build();
            }
            if(param != null){  //修改map
                trigger.getJobDataMap().putAll(param);
            }
            // 按新的trigger重新设置job执行
            scheduler.rescheduleJob(triggerKey,trigger);
        } catch (SchedulerException e) {
            log.error("更新任务失败",e);
        }
    }

    @Override
    public void deleteJob(String jobName, String groupName) {
        try {
            //暂停、移除、删除
            scheduler.pauseTrigger(TriggerKey.triggerKey(jobName,groupName));
            scheduler.unscheduleJob(TriggerKey.triggerKey(jobName,groupName));
            scheduler.deleteJob(JobKey.jobKey(jobName,groupName));
        } catch (SchedulerException e) {
            log.error("删除任务失败",e);
        }
    }

    @Override
    public void startAllJobs() {
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            log.error("开启所有的任务失败",e);
        }
    }

    @Override
    public void pauseAllJobs() {
        try {
            scheduler.pauseAll();
        } catch (SchedulerException e) {
            log.error("暂停所有任务失败",e);
        }
    }

    @Override
    public void resumeAllJobs() {
        try {
            scheduler.resumeAll();
        } catch (SchedulerException e) {
            log.error("恢复所有任务失败",e);
        }
    }

    @Override
    public void shutdownAllJobs() {
        try {
            if(scheduler.isShutdown()){
                //scheduler生命周期结束，无法再 start() 启动scheduler
                scheduler.shutdown(true);
            }
        } catch (SchedulerException e) {
            log.error("关闭所有任务失败",e);
        }
    }
}
